/*
 * @author:
 * @description:
 */
import BaseLayerSwitcherConfig from "@/widgets/BaseLayerSwitcher/js/config";
import imageDataApi from "../../js/api/imageDataApi";
let initialSpot = null;
let toPoint = null;
let orientation = null;
let alreadyLayer = null;
let jumpSpotEnable = null;
var MapInit = {
  async createInitialSpot() {
    let params = { dirCode: "AgCIM-viewPoints" };
    let res = await imageDataApi.selectInitialSpotByUser(params);
    if (res.success && res.content.length > 0) {
      let json = JSON.parse(res.content[0].imageStoreInformation);
      initialSpot = new Cesium.Cartesian3(
        json.position.x,
        json.position.y,
        json.position.z
      );
      jumpSpotEnable = res.content[0].jumpSpotEnable;
      // this.store.commit("initialSpot", information);
      //有默认视点
      if (initialSpot) {
        toPoint = initialSpot;
        orientation = {
          heading: json.heading,
          pitch: json.pitch,
          roll: json.roll,
        };
        alreadyLayer = json.layerId || [];
      }
    } else {
      //没有默认视点的情况
      toPoint = new Cesium.Cartesian3.fromDegrees(
        113.32445849,
        23.12016038,
        6000
      );
    }
  },
  //加载图层 视点图层
  addLayer(data) {
    this.removeLayer(data); //加载图层前，先清理已有的
    let currentLayer = JSON.parse(JSON.stringify(CIM.layerTree._aglayerIds));
    currentLayer = this.uniqueArr(currentLayer);
    let sameArr = this.getSameArray(data, currentLayer);
    let diffArr = this.getDiffArray(data, sameArr);
    diffArr.forEach((item) => {
      CIM.layerTree.layerTreeDom.setLayerCheck(item, true);
    });
  },
  //清理图层，只是处理不同图层，相同则不处理
  removeLayer(addData) {
    let currentLayer = JSON.parse(JSON.stringify(CIM.layerTree._aglayerIds));
    currentLayer = this.uniqueArr(currentLayer);
    let sameArr = this.getSameArray(addData, currentLayer);
    let diffArr = this.getDiffArray(sameArr, currentLayer);

    diffArr.forEach((item) => {
      CIM.layerTree.layerTreeDom.setLayerCheck(item, false);
    });
  },
  //获取两个数组相同部分
  getSameArray(arrPre, arrNext) {
    let res = [];
    for (let i = 0; i < arrNext.length; i++) {
      for (let j = 0; j < arrPre.length; j++) {
        if (arrPre[j] === arrNext[i]) {
          res.push(arrPre[j]);
        }
      }
    }
    return res;
  },
  //获取两个数组不同部分
  getDiffArray(arrPre, arrNext) {
    return arrPre.concat(arrNext).filter(function (v, i, arr) {
      return arr.indexOf(v) === arr.lastIndexOf(v);
    });
  },
  //数组去重
  uniqueArr(arr) {
    return Array.from(new Set(arr));
  },
  creatMap(domDiv, data, userInfo, mapVM) {
    console.log('创建地图');
    let fromPoint = new Cesium.Cartesian3.fromDegrees(
      113.31545849,
      23.12416038,
      24212642.887812868
    ); //初始点 第一个定位
    // debugger
    agcim.scene.mapInit.createMap(domDiv, data, {
      fromPoint: fromPoint,
      toPoint: toPoint,
      orientation: orientation,
      alreadyLayer: alreadyLayer,
      jumpSpotEnable: jumpSpotEnable
    });
    
    var viewer = CIM.viewer;
    window.CIMViewerPromise.resolve(viewer);
    //TODO：关闭手动渲染，手动渲染开启导致很多渲染问题
    //TODO：自定义天空盒
    viewer.scene.skyBox = new Cesium.AgSkyBox({
      sources: viewer.scene.skyBox.sources,
    });
    CIM.viewer.scene.requestRenderMode = false;
    var layers = viewer.imageryLayers._layers;
    //后台配置的底图
    if (layers.length > 0) {
      var layer = layers[0];
      viewer.imageryLayers.remove(layer);
      //将默认图层设置为【必应底图】
      BaseLayerSwitcherConfig.setDefaultLayer(mapVM).then(() => {
        if (BaseLayerSwitcherConfig.defaultLayer[0].name !== "椭球平面") {
          BaseLayerSwitcherConfig.defaultLayer[0].creationFunction(viewer);
        }
        BaseLayerSwitcherConfig.defaultLayer[1].creationFunction(viewer,0);
      });
    }
    //加载默认视点图层
    if (alreadyLayer) {
      setTimeout(()=>{
        this.addLayer(alreadyLayer);
      },3000) 
    }
    CIM.viewer.clock.currentTime.secondsOfDay = 50400;
  },
};
export { MapInit };
